package com.wen.ucenter.controller;


import com.wen.commonutils.GuLiUtils;
import com.wen.commonutils.ResultEntity;
import com.wen.commonutils.ordervo.OrderUcenterMember;
import com.wen.ucenter.entity.Member;
import com.wen.ucenter.entity.vo.MemberVO;
import com.wen.ucenter.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wen
 * @since 2021-06-14
 */
@RestController
@RequestMapping("/edu/ucenter/service/member")
public class MemberController {
    @Resource
    private MemberService memberService;


    /**
     * 登录业务
     *
     * @param member 含手机号和密码
     * @return 成功或失败
     */
    @PostMapping("login")
    public ResultEntity login(@RequestBody Member member) {
        String token = memberService.login(member);
        return ResultEntity.ok().data("token", token);
    }

    /**
     * 注册业务
     *
     * @param memberVO 用户注册信息
     * @return 成功或失败
     */
    @PostMapping("register")
    public ResultEntity register(@RequestBody MemberVO memberVO) {
        memberService.register(memberVO);
        return ResultEntity.ok();
    }

    /**
     * 根据token获取用户id
     *
     * @param request 请求对象
     * @return ResultEntity
     */
    @GetMapping("getMemberInfo")
    public ResultEntity getMemberInfo(HttpServletRequest request) {
        String id = GuLiUtils.getMemberIdByJwtToken(request);
//        根据id查信息
        Member member = memberService.getById(id);
        return ResultEntity.ok().data("member", member);
    }

    /**
     * 根据用户id查询用户信息
     *
     * @param memberId id
     * @return UcenterMemberOrder
     */
    @GetMapping("getMemberInfoById/{memberId}")
    public OrderUcenterMember getMemberInfoById(@PathVariable("memberId") String memberId) {
        OrderUcenterMember orderUcenterMember = new OrderUcenterMember();
        Member member = memberService.getById(memberId);
        BeanUtils.copyProperties(member, orderUcenterMember);
        return orderUcenterMember;
    }

    /**
     * 统计某一天的注册人数
     * @param day target day
     * @return result of operation
     */
    @GetMapping("countRegister/{day}")
    public ResultEntity countRegister(@PathVariable("day") String day) {
        int count = memberService.countRegister(day);
        return ResultEntity.ok().data("countRegister", count);
    }
}

